package de.ret_world.openrpgstudio.engine;

import com.artemis.EntitySystem;
import com.artemis.World;
import de.ret_world.openrpgstudio.engine.artemis.systems.RenderSystem;
import org.jrabbit.standard.game.world.background.Background;

/**
 *
 * @author Emu
 */
public class EntityWorld extends org.jrabbit.standard.game.world.World {

    public static float timescale = 1;
    
    private World entityWorld;
    private EntitySystem renderSystem;

    public EntityWorld(World entityWorld) {
        this.entityWorld = entityWorld;
        renderSystem = entityWorld.getSystemManager().getSystem(RenderSystem.class);
        
        background = new Background();

//        Entity entity = entityWorld.createEntity();
//        entity.addComponent(new Location("test", "bg", new Point(50f, 50f)));
//        entity.addComponent(new Player());
//        entity.addComponent(new SkinComponent(new RenderedTextSkin("( )")));
//        entity.refresh();

        camera.makeActive();
        camera.location().set(-16, -16);
    }
    
    @Override
    public void update(int delta) {
        delta = (int) (delta * timescale);
        
        camera.update(delta);
        background.update(delta);

        entityWorld.loopStart();
        entityWorld.setDelta(delta);

        for (int i = 0; i < entityWorld.getSystemManager().getSystems().size(); i++) {
            entityWorld.getSystemManager().getSystems().get(i).process();
        }
    }

    @Override
    public void render() {
        background.render();
        camera.bind();
        renderSystem.process();
        camera.release();
    }
}
